import { defineStore } from 'pinia'
import { db } from '@renderer/store/db/db'

const useDbSettingStore = defineStore('dbSetting', {
  state: () => {
    return {
      scanPositions: {},
      protocols: {},
      preMonitorProtocol: {} as any,
      monitorProtocol: {} as any
    }
  },
  actions: {
    async getScanPositions() {
      if (this.scanPositions === null) {
        return this.scanPositions
      } else {
        const arr = await db.scanParts.toArray()
        let map = {}
        arr.map(item => {
          map[item.personType] = item.parts
        })
        this.scanPositions = map
        return this.scanPositions
      }
    },
    async getProtocols() {
      if (this.protocols === null) {
        return this.protocols
      } else {
        this.protocols = await db.protocols.toArray()
        return this.protocols
      }
    },
    async getPreMonitorInfoProtocol() {
      if (this.preMonitorProtocol === null) {
        return this.preMonitorProtocol
      } else {
        const arr = await db.preMonitorProtocol.toArray()
        this.preMonitorProtocol = arr[0]
        return this.preMonitorProtocol.params
      }
    },
    async getMonitorInfoProtocol() {
      if (this.monitorProtocol === null) {
        return this.monitorProtocol
      } else {
        const arr = await db.monitorProtocol.toArray()
        this.monitorProtocol = arr[0]
        return this.monitorProtocol.params
      }
    }

  }
})

export {
  useDbSettingStore
}

